<?php
/**
 * Nweb Framework
 *
 * This script is protected by copyright. It's use, copying, modification
 * and distribution without written consent of the author is prohibited.
 *
 * @category    source
 * @package     pl.nweb.framework.application.resource
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2011 Krzysztof Kardasz
 * @license     http://www.gnu.org/licenses/lgpl-3.0.txt  GNU Lesser General Public
 * @version     3.0 $Id: bootstrap.php 461 2012-01-13 09:14:12Z krzysztof.kardasz $
 * @link        http://code.google.com/p/nweb-framework/
 * @link        http://framework.nweb.pl
 */

namespace pl\nweb\framework\application\resource;

/**
 * Kreator obiektu Bootstrap
 *
 * @category    source
 * @package     pl.nweb.framework.application.resource
 * @author      Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright   Copyright (c) 2011 Krzysztof Kardasz
 * @version     3.0 $Id: bootstrap.php 461 2012-01-13 09:14:12Z krzysztof.kardasz $
 */
class Bootstrap implements iResource
{
    /**
     * Klasa Bootstrap
     *
     * @var string
     */
    private $_class = '\\pl\\nweb\\framework\\application\\Bootstrap';

    /**
     * Opcje
     *
     * @var array
     */
    private $_options = array();

    /**
     * Obiekt klasy Bootstrap
     *
     * @var \pl\nweb\framework\application\Bootstrap
     */
    private $_bootstrap = null;

    /**
     * Opcje
     */
    const OPTION_CLASS   = 'class';
    const OPTION_OPTIONS = 'options';

    /**
     * Konstruktor
     *
     * @param  array|string|null $config konfiguracja
     */
    public function __construct ($config = null)
    {
        if(null !== $config) {
            if(is_string($config)) {
                $this->_class = $config;
            } else {
                if(isset($config[self::OPTION_CLASS])) {
                    $this->_class = $config[self::OPTION_CLASS];
                }
                if(isset($config[self::OPTION_OPTIONS])) {
                    if(!is_array($config[self::OPTION_OPTIONS])) {
                        $config[self::OPTION_OPTIONS] = (array)$config[self::OPTION_OPTIONS];
                    }
                    $this->_options = $config[self::OPTION_OPTIONS];
                }
            }
        }
    }

    /**
     * Definiuje opcje
     *
     * @param array|string $name nazwa
     * @param null|mixed $value wartość
     * @return void
     */
    public function setOption ($name, $value = null)
    {
        if(is_array($name)) {
            $this->_options = array_merge($this->_options, $name);
        } else {
            $this->_options[$name] = $value;
        }
    }

    /**
     * Zwraca obiekt klasy Bootstrap
     *
     * @return \pl\nweb\framework\application\Bootstrap
     * @throws \pl\nweb\framework\application\resource\Exception
     */
    public function getObject ()
    {
        if(null === $this->_bootstrap) {
            if(!class_exists($this->_class, true)) {
                throw new Exception('Bootstrap class "' . $this->_class . '" not found');
            }
            $class = $this->_class;
            $obj   = new $class ($this->_options);
            if(!$obj instanceof \pl\nweb\framework\application\Bootstrap) {
                throw new Exception('Bootstrap class "' . $class . '" must be instance of "\pl\nweb\framework\application\Bootstrap"');
            }
            $this->_bootstrap = $obj;
        }
        return $this->_bootstrap;
    }
}